<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * sitech
 *
 * @package		sitech
 * @author		SITECH : Dev Team - Chim Rath
 * @license		http://sitech.com.kh
 * @link		http://sitech.com.kh
 * @since		Version 0.9.4
 */

// ------------------------------------------------------------------------


/**
 * sitech Media Model
 *
 * @package		sitech
 * @subpackage	Models
 * @category	All Models
 * @author		SITECH : Dev Team - Chim Rath
 *
 */

class pr_product_model extends Base_model 
{
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	public $pd_id;
	//public $db_sidb;
	public function __construct()
	{
		parent::__construct();

		$this->pd_id	= 	'id';	 
		//$this->code		= 	'code';	
			
	}

	/**
	 * Returns the list
	 *
	 */
	function get_all_product()
	{
		$this->db->select('*');
		$this->db->from('tbl_product');
		//$this->db->join('tbl_category AS c', 'p.cat_id = c.cat_id');
		$query = $this->db->get();
		if ( $query->num_rows() > 0 )
			$data = $query->result_array();
		return $data;
	}
	//get data by sql
	function get_by_sql($sql)
	{
		$query	= $this->db->query($sql);
		//trace($this->db->queries);		
		if(!empty($query))
		{
			$results = array();
			if ( $query->num_rows() > 0 )
				$results = $query->result_array();
			return $results;	
		}
	}
	//select one product
	function get_one_row($table,$data)
	{	
		$this->db->where($data);
		
		$query = $this->db->get($table);

		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}
	}
	/**
	 * Count row in table
	 *
	 */
	function count_rows($table, $where)
	{	
		$this->db->where($where);
		$query = $this->db->get($table);
		if(!empty($query)){	
			$count = $query->num_rows();
			return $count;
		}	
		
	}
	function count_by_sql($sql)
	{
		$query	= $this->db->query($sql);
		//trace($this->db->queries);
		if(!empty($query))
		{
			$count = $query->num_rows();
			return $count;	
			
		}
	}
	//insert product
	function insert($table, $data)
	{   
		$affected_rows	= $this->db->insert($table, $data);
		return $affected_rows;
	}
	function save_edit_product($product_info,$product_id)
	{
		$this->db->where('pd_id',$product_id);
		return $this->db->update('tbl_product',$product_info);
	}
	// ------------------------------------------------------------------------
	//delete product
	function delete($table, $data)
	{
		$affected_rows	= $this->db->delete($table, $data);
		//trace($this->db_ipinfo->queries);
		return $affected_rows;
	}
	/**
	 * Get by filter
	 *
	 */
	function get_by_filter($table, $posts = FALSE, $orderby = FALSE, $options = FALSE, $limit = FALSE, $offset = FALSE)
	{	
		if($posts != FALSE)
		{
			$datas = array();
			// Get Fields Name & Data Type of Fields
			$fields	= $this->get_datatype(array('table_name' => $table));
			
			foreach($fields as $field)
			{
				if(!empty($posts[$field['column_name']]))
				{	
					if(is_array($posts[$field['column_name']]))
					{
						$this->db->where_in($field['column_name'], $posts[$field['column_name']]);
					}
					else
					{
						$datas[$field['column_name']] = $posts[$field['column_name']];
						
						if($field['data_type'] == 'integer')
							$this->db->where($field['column_name'], $posts[$field['column_name']]);
						else
							$this->db->like('lower('.$field['column_name'].')',strtolower($posts[$field['column_name']]));
					}
				}
			}
		}
		
		if($orderby == FALSE)
			$this->db->order_by($this->pk_name,$options);
		else
			$this->db->order_by($orderby,$options);
		
		if( $limit == FALSE && $offset == FALSE)
			$query = $this->db->get($table);
		else
			$query = $this->db->get($table,$limit,$offset);
//trace($this->db->queries);
		if(!empty($query))
		{	
			$data = array();
			if ( $query->num_rows() > 0 )
				$data = $query->result_array();
			return $data;
		}	
	}	
	/**
	 * Get Data Type of Fileds
	 *
	 */ 	 
	function get_datatype($data)
	{	
		$this->db->select('column_name,data_type');		
		$query = $this->db->get_where('information_schema.columns', $data);
		
		if(!empty($query))
		{	
			$results = array();
			
			if ( $query->num_rows() > 0 )
				$results = $query->result_array();
			
			return $results;
		}
	}	
} // End Class
/* End of file models_model.php */
/* Location: ./application/models/models_model.php */